package at.jku.ssw.fortress.ui;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import com.sun.fortress.nodes.Component;
import com.sun.fortress.nodes.Decl;
import com.sun.fortress.nodes.FnDecl;
import com.sun.fortress.nodes.Node;
import com.sun.fortress.nodes.NodeAbstractVisitor_void;
import com.sun.fortress.nodes.ObjectDecl;
import com.sun.fortress.nodes.TraitDecl;
import com.sun.fortress.nodes.VarDecl;

public class ParentVisitor extends NodeAbstractVisitor_void {
	
	private Map<Node,Node> parent = new HashMap<Node, Node>();
	private Stack<Node> stack = new Stack<Node>();
	
	@Override
	public void forComponent(Component x) {
		stack.push(x);
		for (Decl d : x.getDecls()) {
		    forDecl(d);
		}
		stack.pop();
	}
	
	@Override
	public void forTraitDecl(TraitDecl x) {
		parent.put(x, stack.peek());
		stack.push(x);
		for (Decl d : x.getDecls()) {
	            forDecl(d);
		}
		stack.pop();
	}
	
	@Override
	public void forObjectDecl(ObjectDecl x) {
		parent.put(x, stack.peek());
		stack.push(x);
		for (Decl d : x.getDecls()) {
		    forDecl(d);
		}
		stack.pop();
	}
	
	@Override
	public void forFnDecl(FnDecl x) {
		parent.put(x, stack.peek());
	}
	
	@Override
	public void forVarDecl(VarDecl x) {
		parent.put(x, stack.peek());
	}

	
	public Map<Node,Node> parents() {
		return parent;
	}
}
